package com.springboot2.controller;


import com.springboot2.entity.GoodsCategory;
import com.springboot2.service.GoodsCategoryService;
import com.springboot2.utils.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("category/")
public class GoodsCategoryController {

    @Resource
    private GoodsCategoryService goodsCategoryService;
    //增
    @PostMapping("add")
    public Result add(@RequestBody GoodsCategory goodsCategory){
        goodsCategoryService.add(goodsCategory);
        return Result.success();
    }
    //删
    @DeleteMapping("del/{id}")
    public Result del(@PathVariable int id){
        goodsCategoryService.del(id);
        return Result.success();
    }
    //改
    @PutMapping("update")
    public Result update(@RequestBody GoodsCategory goodsCategory){
        goodsCategoryService.update(goodsCategory);
        return Result.success();
    }
    //获取对象
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable int id){
        return Result.success(goodsCategoryService.findById(id));
    }
    //分页列表
    @GetMapping("findByPage/{pageNo}/{pageSize}")
    public Result findByPage(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("data",goodsCategoryService.findByPage(pageNo, pageSize));
        map.put("total",goodsCategoryService.count());
        return  Result.success(map);
    }
    //查询条件
    @GetMapping("findByPageParams/{pageNo}/{pageSize}/{title}")
    public Result findByPageParams(@PathVariable int pageNo,@PathVariable int pageSize,@PathVariable String title){
        Map<String,Object> map = new HashMap<>();
        map.put("data",goodsCategoryService.findByPageParams(pageNo, pageSize, title));
        map.put("total",goodsCategoryService.countParams(title));
        return  Result.success(map);
    }
    //查询条件
    @GetMapping("findAll")
    public Result findAll(){
        return  Result.success(goodsCategoryService.findAll());
    }
    //查询条件总记录
    @DeleteMapping("batch/{ids}")
    @Transactional
    public Result batch(@PathVariable String ids){
        String [] idsArray= ids.split(",");
        for(String id:idsArray){
            goodsCategoryService.del(Integer.parseInt(id));
        }
        return Result.success();
    }

}
